/*
 [The "BSD license"]
 Copyright (c) 2011-2011 Joel Li (李家智)
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
 3. The name of the author may not be used to endorse or promote products
     derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.bee.tl.core;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.MessageFormat;

import org.antlr.runtime.ANTLRReaderStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;

/*一个简单扩展，只用来衡量表达式*/
public class SimpleRuleEval extends BeeTemplate {
	public SimpleRuleEval(String input) {
		super("var k=(" + input + ");");
	}

	public SimpleRuleEval(String input, String pattern) {

		super(MessageFormat.format(pattern, new String[] { input }));

	}

	protected void parse() throws IOException {
		if (!isParsed) {

			if (this.group == null) {
				this.group = new GroupTemplate();
			} else {
				copyGroupPropertie();
			}

			this.context.set("__group", group);
			this.context.set("__this", this);

			tf = new Transformator(placeholderStart, placeholderEnd,
					this.statementStart, this.statementEnd);

			if (resource == null) {
				// 是字符流，不得不记在内存中
				resource = new Resource(this.input);
			}
			resource.setCR("\n");

			antlrReader = new ANTLRReaderStream(this.reader);
			lexer = new BeeLexer(antlrReader);
			tokens = new CommonTokenStream(lexer);
			parser = new BeeParser(tokens);
			parser.setTreeAdaptor(new BeeNodeAdaptor());
			BeeParser.prog_return r = null;
			try {
				r = parser.prog();
				this.isParseSuccess = true;
			} catch (MVCStrictException ex) {
				this.isParseSuccess = false;
				this.lasetRe = ex;
				return;
			} catch (RecognitionException e) {
				e.printStackTrace();
				this.lasetRe = e;
				this.isParseSuccess = false;
				return;
			}

			tree = (BeeCommonNodeTree) r.getTree();
			isParsed = true;
			this.reader.close();
			tf = null;

		}

		// System.out.println(tree.toStringTree());
		// System.out.println(tf.getTextMap());
	}

	public Object calc() {

		return calc("k");

	}

	public Object calc(String name) {

		try {
			getTextAsString();
		} catch (IOException e) {
			throw new RuntimeException("计算错误");
		} catch (BeeException ex) {
			throw new RuntimeException("计算错误,格式不对或者运行时候出错");
		}

		Object o = this.context.getVar(name);
		return o;
	}

	public boolean calcBoolean() {
		return (Boolean) calc();
	}

	public int calcInt() {
		return ((BeeNumber) calc()).intValue();
	}

	public long calcLong() {
		return ((BeeNumber) calc()).longValue();
	}

	public double calcDouble() {
		return ((BeeNumber) calc()).doubleValue();
	}

	public double calcFloat() {
		return ((BeeNumber) calc()).floatValue();
	}

	public BigDecimal calcBigDecimal() {
		return ((BeeNumber) calc()).thisNumber;
	}

	public static void main(String[] args) throws IOException {
		SimpleRuleEval eval = new SimpleRuleEval("100000000000/223");
		Object o = eval.calc();
		System.out.println(o);
	}

}
